/***********************************************************************
 * hash table functions
 * creates a hash / object data structure such as,
 * hashObj = {
 		timePeriod: {date, hour, daylight, goNoGo, temperature, rain, wind}
    	0107: { date: 01,
      			hour: 07,
   				daylight: true,
    	    	goNoGo: 2,				// None:0, Red:1, Yellow:2, Green:3
    			temperature: 12,
     			rain: 7,
     			wind: 5
   		 		}
	};
	var obj = {
	    name: "Carrot",
    	"for": "Max",
    	details: {
        	color: "orange",
        	size: 12
      	}
	};
*/

Array.prototype.maxIt = function(){
	return Math.max.apply({},this)
}

function Hash() {
	/* init hash table */
	this.hashArr = new Object();
}
	
Hash.prototype.putRec = function( recKey, record ) {
	/* putRec - loads the record object into the hash table */
	for( item in record ) this.putItem( recKey, item, record[item] );
};
	
Hash.prototype.len = function(recKey) {
	/* len - number of records in the hash */
	var length=0;
	for( var item in this.hashArr[recKey] ) length++;
	return length;
};
	
Hash.prototype.putItem = function( recKey, item, value ) {
	/* put items into the hash table */
	if ( typeof( this.hashArr[recKey] ) == 'undefined' ) {
		this.length ++;
		this.hashArr[recKey] = new Object();
	}
	this.hashArr[recKey][item] = value;
};
	
Hash.prototype.getHash = function() {
	/* getHash - returns the hash table */
	return this.hashArr;
};
	
Hash.prototype.getItem = function(recKey, item) {
	/* get items from hash table */
//	if( this.has(recKey) ) return this.hashArr[recKey].get(item);
	if( this.has(recKey) ) return this.hashArr[recKey][item];
  };
	
Hash.prototype.remove = function(recKey) {
	/* remove entries from the hash table */
//debug("removing (" + recKey + "): " + this.recToString(recKey) );
	if( this.has(recKey) ) {
		this.length --;
		var value = this.hashArr[recKey];
		delete this.hashArr[recKey];
		return value;
	}
};
	
Hash.prototype.has = function(recKey) {
	/* determine if a recKey is in the hash table */
	return ( typeof( this.hashArr[recKey] ) != 'undefined' );
};
	
	
Hash.prototype.view = function() {
	/* view the hash */
	debug( 'Hash view' + this.toString() );
};
	
Hash.prototype.recToString = function(recKey) {
	/* view hash table entries */
	var str = '';
	var row = this.hashArr[recKey];
	str += recKey + ' => ';
	for( var item in row ) str += item + ': ' + row[item] + ', ';
	return str.replace(/, $/,"");
};
	
Hash.prototype.toString = function() {
	/* view hash table entries */
	var str = '';
	for( var recKey in this.hashArr ) str += this.recToString(recKey);
	return str.replace(/, $/,"");
};
	
	
Hash.prototype.sort = function() {
	/* returns the sorted hash table entries by the recKeys */
	var newArr = new Hash();
	var tmpArr = new Array();
	for( var recKey in this.hashArr ) tmpArr.push(recKey);
	tmpArr.sort();		// sort just the record keys
	for( var index = 0; index < tmpArr.length; index++ ){
		var dateHourKey = tmpArr[index];
		var getRec = this.hashArr[dateHourKey];
		newArr.putRec( dateHourKey, getRec );
	}
	return newArr;
};
	
	
Hash.prototype.winnow = function() {
	/* removes the short rows from the hash table  */
//debug( 'hash winnow' );
	for( i in this.hashArr ){
//debug( 'this.hashArr[' + i + '].length: ' + this.len(i) );
		if( this.len(i) < 7 ) this.remove(i);
	}
};
	
	
Hash.prototype.setGoNoGo = function() {
	/* assesses the overall goNoGo for the time period  */
	for( var dateHour in this.hashArr ){
		var intArr = new Array(); 
		intArr.push( this.getItem(dateHour,'temperatureGoNoGo') || goNoGoType.None );
		intArr.push( this.getItem(dateHour,'rainGoNoGo') || goNoGoType.None );
		intArr.push( this.getItem(dateHour,'windGoNoGo') || goNoGoType.None );
		var theMax = intArr.maxIt();
//debug( 'intArr toString ' + intArr.toString() + '==>' + theMax );
		this.putItem( dateHour, "maxGoNoGo", theMax );
	}
};

